package com.stockz.ui.swing.charting.painter.axis;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.stockz.ui.swing.charting.util.MathUtil;
import com.stockz.ui.swing.charting.view.StockChartPanel;
import com.stockz.ui.swing.charting.view.StockChartPanelSkin;
import com.stockz.ui.swing.charting.view.StockChartPanelState;

public class TimeAxisPainter{

	private static int[] currentXmarks;
	
	@SuppressWarnings("deprecation")
	public static void paint(Graphics2D g, StockChartPanel stockChartPanel,  Rectangle rectangle) {
		StockChartPanelState state = stockChartPanel.getStockChartPanelState();
		StockChartPanelSkin skin = stockChartPanel.getStockChartPanelSkin();
		
		g.setColor(skin.getAxisTimeColor());
		g.drawLine(rectangle.x, rectangle.y, rectangle.x + rectangle.width, rectangle.y);
		int start = state.getStartIndex();
		
		if(state.getBarList() == null || state.getBarList().isEmpty()){
			return;
		}

		int prevMonth = state.getBarList().get(start).getDate().getMonth();
		int prevYear = state.getBarList().get(start).getDate().getYear(); 
		int lastTextX = 0;
		List<Integer> intList = new ArrayList<Integer>();
		for(int i = state.getEndIndex()-1; i >= state.getStartIndex(); i--){
			int x = MathUtil.fitX(rectangle.x, state.getTimeUnitSpace(), state.getEndIndex(), i);
			g.setColor(skin.getAxisTimeColor());
			g.drawLine(x, rectangle.y, x, rectangle.y + skin.getShortTickLength());
			g.setColor(skin.getTextColor());
			g.setFont(skin.getSmallFont());
				Date date = state.getBarList().get(i).getDate();
				int month = date.getMonth();
				int year = date.getYear();
				String text = null;

				if(prevYear != year){
					text = String.valueOf(date.getYear()+1900);
				}else if(prevMonth != month){
					text = getTextMonth(month);
				}else if(state.getTimeUnitSpace() > 12){
					text = String.valueOf(date.getDate());
				}
				
				if(text != null){
					int nextTextX = (int)(x+g.getFont().getStringBounds(text, g.getFontRenderContext()).getWidth());
					
					if(x > lastTextX + 5 && nextTextX <= rectangle.x+rectangle.width){
						prevMonth = month;
						prevYear = year;
						g.drawString(text, x, rectangle.y+14);
						
						if(x > rectangle.x && x < rectangle.x+rectangle.width){
							intList.add(x);
						}
						
						lastTextX = nextTextX;
					}
				}
				
			}
		currentXmarks = new int[intList.size()];
		for(int i = 0 ; i < intList.size(); i++){
			currentXmarks[i] = intList.get(i);
		}
		}
		
	private static String getTextMonth(int month){
		switch(month){
		case 1 : return "FEB";
		case 2 : return "MAR";
		case 3 : return "APR";
		case 4 : return "MAY";
		case 5 : return "JUN";
		case 6 : return "JUL";
		case 7 : return "AUG";
		case 8 : return "SEP";
		case 9 : return "OCT";
		case 10 : return "NOV";
		case 11 : return "DEC";
		default : return "";
		}
	}

	public static int[] getCurrentXmarks() {
		return currentXmarks;
	}
}